package com.ewzj.bigdata.mvp.presenter

import android.app.Activity
import android.app.Application
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.HomeFragmentContract
import com.ewzj.bigdata.mvp.contract.LogoContract
import com.ewzj.bigdata.mvp.contract.NewFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.CityBean
import com.ewzj.bigdata.mvp.model.rspbean.InfoBean
import com.ewzj.bigdata.mvp.model.rspbean.InfosData
import com.ewzj.bigdata.mvp.model.rspbean.RspOriginBean
import com.ewzj.bigdata.mvp.ui.adapter.InfoAdapter
import com.ewzj.bigdata.mvp.ui.adapter.OriginAdapter
import com.ewzj.bigdata.mvp.ui.adapter.TabAdapter
import com.ewzj.bigdata.mvp.ui.fragment.NewFragment
import com.ewzj.bigdata.mvp.ui.fragment.OrginNewsFragment
import com.ewzj.bigdata.mvp.ui.fragment.RealNewsFragment
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class NewFragmentPresenter @Inject constructor(val mErrorHandler: RxErrorHandler, val application: Application, view: NewFragmentContract.View, model: NewFragmentContract.Model,val mGson: Gson):BasePresenter<NewFragmentContract.Model,NewFragmentContract.View>(model,view) {
    var orginnewdatas=ArrayList<RspOriginBean>()
    var typenewdatas=ArrayList<RspOriginBean>()
    var infonewdatas=ArrayList<InfoBean>()
    var orginadapter=OriginAdapter(orginnewdatas)
    var typeadapter=OriginAdapter(typenewdatas)
    var timenewadapter=InfoAdapter(infonewdatas)
    var page=1
    var infopage=1
    var locationcity: String = ""
    val context=(mRootView as Fragment).context
    var requesttype:Int?=1
    fun initRecyadapter(){
        orginadapter= OriginAdapter(orginnewdatas)
        mRootView.initRecylerview(orginadapter)
        timenewadapter.setOnLoadMoreListener(object : BaseQuickAdapter.RequestLoadMoreListener {
            override fun onLoadMoreRequested() {
                loadmoreInfoData()
            }

        })
        typeadapter.setOnLoadMoreListener(object : BaseQuickAdapter.RequestLoadMoreListener {
            override fun onLoadMoreRequested() {
                loadmoreTypeData()
            }

        })
//        timenewadapter.disableLoadMoreIfNotFullPage()
        orginadapter.disableLoadMoreIfNotFullPage()
    }
    fun switchAdapter(recyclerView: RecyclerView,type: Int?){
        when(type)
        {
            1->{
                recyclerView.adapter=orginadapter

            }
            2->{
                recyclerView.adapter=timenewadapter
            }
            3,4,5,6->{
                recyclerView.adapter=typeadapter
            }
        }

    }
    fun setRequestType(type:Int?){
        requesttype=type
    }
    fun getFirstData(){
        val pagebean= Reqpagebean()
        page=1
        mModel.getnewsByPage(pagebean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<List<RspOriginBean>>(mErrorHandler){
                    override fun onNext(t: List<RspOriginBean>) {
                        page++
                        orginnewdatas.clear()
                        orginnewdatas.addAll(t)
                        orginadapter.setNewData(orginnewdatas)
                        if (t.size<AppKey.PAGE_NUM){
                            orginadapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }
    fun loadmoreData(){
        val pagebean= Reqpagebean(page)
        mModel.getnewsByPage(pagebean).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (orginadapter.isLoading){
                    orginadapter.loadMoreComplete()
                } }
                .subscribe(object :Consumer<List<RspOriginBean>>{
                    override fun accept(t: List<RspOriginBean>) {
                        page++
                        if (t.size<AppKey.PAGE_NUM){
                            orginadapter.addData(t)
                            orginadapter.loadMoreEnd()
                        }else{
                            orginadapter.addData(t)
                        }
                    }
                }, Consumer { orginadapter.loadMoreFail() })
    }
    fun onreusme(){
        var spcity = DataHelper.getStringSF(context, AppKey.LOCATION_CITY)
        if (spcity.isNullOrEmpty()) return
        if (!locationcity.equals(spcity)) {
            var citybean = mGson.fromJson(spcity, CityBean.ListEntity::class.java)
            getFirstData()
            initInfoFirstData()
            initTypeData()
            locationcity = DataHelper.getStringSF(context, AppKey.LOCATION_CITY)
        }
    }
    fun initInfoFirstData():Unit{
        infopage=1
        mModel.getInfos(Reqpagebean()).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<InfosData>(mErrorHandler){
                    override fun onNext(t: InfosData) {
                        infopage++
                        timenewadapter.setNewData(t.list)
                        if (t.list.size<AppKey.PAGE_NUM){
                            timenewadapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }

    fun loadmoreInfoData():Unit{
        mModel.getInfos(Reqpagebean(infopage)).compose(RxUtils.bindToLifecycle(mRootView))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (timenewadapter.isLoading){
                    timenewadapter.loadMoreComplete()
                } }
                .subscribe(object :Consumer<InfosData>{
                    override fun accept(t: InfosData) {
                        infopage++
                        if (t.list.size<AppKey.PAGE_NUM){
                            timenewadapter.addData(t.list)
                            timenewadapter.loadMoreEnd()
                        }else{
                            timenewadapter.addData(t.list)
                        }
                    }

                },Consumer { timenewadapter.loadMoreFail() })
    }

    fun initTypeData(){
        val pagebean= Reqpagebean()
        page=1
        pagebean.sub_type=requesttype
        mModel.getnewsTypeByPage(pagebean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<List<RspOriginBean>>(mErrorHandler){
                    override fun onNext(t: List<RspOriginBean>) {
                        page++
                        typenewdatas.clear()
                        typenewdatas.addAll(t)
                        typeadapter.setNewData(typenewdatas)
//                        if (t.size<AppKey.PAGE_NUM){
//                            typeadapter.disableLoadMoreIfNotFullPage()
//                        }
                    }

                })
    }
    fun refresh(){
        when(requesttype)
        {
            0->{
                getFirstData()
            }
            1->{
                initInfoFirstData()
            }
            2,3,4,5->{
                initTypeData()
            }
        }
    }
    fun loadmoreTypeData(){
        val pagebean= Reqpagebean(page)
        pagebean.sub_type=requesttype
        mModel.getnewsTypeByPage(pagebean).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (typeadapter.isLoading){
                    typeadapter.loadMoreComplete()
                } }
                .subscribe(object :Consumer<List<RspOriginBean>>{
                    override fun accept(t: List<RspOriginBean>) {
                        page++
                        if (t.size<AppKey.PAGE_NUM){
                            typeadapter.addData(t)
                            typeadapter.loadMoreEnd()
                        }else{
                            typeadapter.addData(t)
                        }
                    }
                }, Consumer { typeadapter.loadMoreFail() })
    }
}
